/**
 * @description 登录页面对应的属性组件
 */

import React, { useEffect, useMemo, useState, useContext, useRef } from 'react'
import { Collapse, Form, Input, Radio } from 'antd';

import { replaceObjectStyle, replaceCssStyle } from '@/utils/style';
import { setAttrForm } from '@/utils/AttrForm';
import { copyTemplate, pasteTemplate, copyBlock, pasteBlock } from '@/utils/clipboard';
import { iconTextReg, FlagOptions } from '@/utils/Const';
import { UpdateContext } from '@/store/updateContext';

import StyleWrapper from '@/components/StylesWrapper';
import CssEdit from '@/components/Monaco/CssEidt';
import AttrHeaderBtn from './Components/AttrHeaderBtn';
import AttrExtra from '@/views/2025/Front/AttrWrapper/Components/AttrExtra';

// 表单布局
const layoutOption = [
  {
    value: 'horizontal',
    label: '水平'
  },
  {
    value: 'vertical',
    label: '垂直'
  },
]

// 标题和后缀现实的开关
const titleShowOptions = [
  {
    label: '开',
    value: true
  },
  {
    label: '关',
    value: false
  }
]


function AddAttr({ template2, onConfigEvent, setIsRightAttr, actKey }) {

  const updateCtx = useContext(UpdateContext)
  const [form] = Form.useForm()
  // css编辑器
  const [open, setOpen] = useState(false)
  // css编辑器实例
  const cssEditRef = useRef()

  // 更新style样式文件
  let upateStyleEvent = (keys) => {

    let divData = Object.assign({}, form.getFieldValue(keys))

    // keys数组的最后一项，就是key
    let key = keys.at(-1)
    replaceObjectStyle(actKey, key, divData)
  }

  // 组件css变化
  let cssChange = (value) => {
    form.setFieldValue('css', value)
    replaceCssStyle(actKey, value)
  }

  // 图标输入框变化事件，比普通输入框，多一个字符串转化逻辑
  let iconEvent = (keys, value) => {

    // 去除 空格、<、/> 
    let newValue = value.replace(iconTextReg, '')
    form.setFieldValue(keys, newValue)

    onConfigEvent(keys, newValue)
  }

  // 头部按钮事件
  let handleHeaderEvent = (key) => {
    switch (key) {

      // 打开组件css编辑
      case 'css':
        setOpen(true)
        break;

      // 复制模板
      case 'copy':
        copyTemplate(actKey, [], form)
        break;

      // 黏贴模板
      case 'paste':
        pasteTemplate(form, onConfigEvent, actKey)
        updateCtx.setUpdateFlag(pre => !pre)

        // 修改css编辑器的值
        cssEditRef.current.setValue(form.getFieldValue('css'))
        break;

      // 切换位置
      case 'position':
        setIsRightAttr((isRight) => !isRight)
        break;

      default:
        break;
    }
  }

  // 属性栏右上角按钮事件
  let handleAttrEvent = (eventKey, blockKey, paramKeys, classKey) => {
    switch (eventKey) {

      // 复制模块
      case 'copy':
        copyBlock(actKey, blockKey, paramKeys, classKey, form)
        break;

      // 黏贴模块
      case 'paste':
        pasteBlock(actKey, blockKey, paramKeys, classKey, form, onConfigEvent)
        updateCtx.setUpdateFlag(pre => !pre)
        break;


      default:
        break;
    }
  }
  // -----------------------------------
  // 构造折叠面板的数据
  // -----------------------------------



  let items = useMemo(() => {
    const items = [
      {
        key: 'box',
        label: '总盒子',
        children: <StyleWrapper form={form} preKeys={['box', 'default']} onStyleChange={() => upateStyleEvent(['box'])} />
      },
      {
        key: 'header',
        label: '头部',
        children: <Collapse accordion items={[
          {
            key: 'box',
            label: '盒子',
            children: <StyleWrapper form={form} preKeys={['header', 'default']} onStyleChange={() => upateStyleEvent(['header'])} />
          },
          {
            key: 'title',
            label: '标题',
            children: <StyleWrapper form={form} preKeys={['header_title', 'default']} onStyleChange={() => upateStyleEvent(['header_title'])} />
          },
        ]} />
      },
      {
        key: 'icon',
        label: '关闭图标',
        children: <Collapse accordion size='middle' items={[
          {
            key: 'icon',
            label: '图标',
            children: <StyleWrapper form={form} preKeys={['close_icon', 'default']} onStyleChange={() => upateStyleEvent(['close_icon'])} />
          },
          {
            key: 'icon-hover',
            label: '图标-悬浮',
            children: <StyleWrapper form={form} preKeys={['close_icon', 'hover']} onStyleChange={() => upateStyleEvent(['close_icon'])} />
          },
        ]} />
      },
      {
        key: 'form',
        label: '表单',
        children:
          <>
            <Form.Item
              label="布局"
              name={['addform', 'base', 'layout']}
            >
              <Radio.Group
                options={layoutOption}
                onChange={(e) => onConfigEvent(['addform', 'base', 'layout'], e.target.value)}
                optionType="button"
                buttonStyle="solid"
              />
            </Form.Item>

            <Collapse accordion size='middle' items={[
              {
                key: 'form',
                label: '表单盒子',
                children:
                  <StyleWrapper form={form} preKeys={['form', 'default']} onStyleChange={() => upateStyleEvent(['form'])} />
              },
              {
                key: "input",
                label: "输入框",
                extra: <AttrExtra onEvent={(eventKey) => handleAttrEvent(eventKey, 'form', ['addform'], 'addform')} />,
                children:
                  <Collapse accordion size='middle' items={[
                    {
                      key: 'item',
                      label: 'item盒子',
                      children: <StyleWrapper form={form} preKeys={['addform', 'addform_item', 'default']} onStyleChange={() => upateStyleEvent(['addform', 'addform_item'])} />,
                    },
                    {
                      key: 'label',
                      label: '标签',
                      children:
                        <>
                          <Form.Item
                            label="开关"
                            name={['addform', 'base', 'labelFlag']}
                          >
                            <Radio.Group
                              options={FlagOptions}
                              onChange={(e) => onConfigEvent(['addform', 'base', 'labelFlag'], e.target.value)}
                              optionType="button"
                              buttonStyle="solid"
                            />
                          </Form.Item>

                          <Form.Item
                            label="冒号开关"
                            name={['addform', 'base', 'colonFlag']}
                          >
                            <Radio.Group
                              options={FlagOptions}
                              onChange={(e) => onConfigEvent(['addform', 'base', 'colonFlag'], e.target.value)}
                              optionType="button"
                              buttonStyle="solid"
                            />
                          </Form.Item>

                          <StyleWrapper form={form} preKeys={['addform', 'addform_label', 'default']} onStyleChange={() => upateStyleEvent(['addform', 'addform_label'])} />

                        </>
                    },
                    {
                      key: 'input',
                      label: '普通',
                      children:
                        <Collapse accordion size='middle' items={[
                          {
                            key: 'input',
                            label: '普通',
                            children:
                              <StyleWrapper form={form} preKeys={['addform', 'addform_input', 'default']} onStyleChange={() => upateStyleEvent(['addform', 'addform_input'])} />,
                          },
                          {
                            key: 'input-focus',
                            label: '聚焦',
                            children:
                              <StyleWrapper form={form} preKeys={['addform', 'addform_input', 'focus']} onStyleChange={() => upateStyleEvent(['addform', 'addform_input'])} />,
                          },
                          {
                            key: 'input-icon',
                            label: '图标',
                            children:
                              <StyleWrapper form={form} preKeys={['addform', 'addform_input_icon', 'default']} onStyleChange={() => upateStyleEvent(['addform', 'addform_input_icon'])} />,
                          },
                          {
                            key: 'input-icon-hover',
                            label: '图标-hover',
                            children:
                              <StyleWrapper form={form} preKeys={['addform', 'addform_input_icon', 'hover']} onStyleChange={() => upateStyleEvent(['addform', 'addform_input_icon'])} />,
                          },
                        ]} />
                    },
                    {
                      key: 'input_placeholder',
                      label: '输入框的提示语',
                      children: <StyleWrapper form={form} preKeys={['addform', 'addform_input_placeholder', 'default']} onStyleChange={() => upateStyleEvent(['addform', 'addform_input_placeholder'])} />,
                    },
                    {
                      key: 'input-disable',
                      label: '禁用的样式',
                      children:
                        <StyleWrapper form={form} preKeys={['addform', 'addform_input_disable', 'default']} onStyleChange={() => upateStyleEvent(['addform', 'addform_input_disable'])} />
                    },
                    {
                      key: 'input_error',
                      label: '输入框错误时',
                      children:
                        <StyleWrapper form={form} preKeys={['addform', 'addform_input_error', 'default']} onStyleChange={() => upateStyleEvent(['addform', 'addform_input_error'])} />
                    },
                    {
                      key: 'number_input',
                      label: '数值',
                      children:
                        <Collapse accordion size='middle' items={[
                          {
                            key: 'input',
                            label: '数值',
                            children:
                              <StyleWrapper form={form} preKeys={['addform', 'addform_number', 'default']} onStyleChange={() => upateStyleEvent(['addform', 'addform_number'])} />,
                          },
                          {
                            key: 'input-focus',
                            label: '聚焦',
                            children:
                              <StyleWrapper form={form} preKeys={['addform', 'addform_number', 'focus']} onStyleChange={() => upateStyleEvent(['addform', 'addform_number'])} />,
                          },
                          {
                            key: 'input-icon',
                            label: '图标盒子',
                            children:
                              <StyleWrapper form={form} preKeys={['addform', 'addform_number_icon_box', 'default']} onStyleChange={() => upateStyleEvent(['addform', 'addform_number_icon_box'])} />,
                          },
                          {
                            key: 'input-icon-up',
                            label: '上图标',
                            children:
                              <StyleWrapper form={form} preKeys={['addform', 'addform_number_icon_up', 'default']} onStyleChange={() => upateStyleEvent(['addform', 'addform_number_icon_up'])} />,
                          },
                          {
                            key: 'input-icon-down',
                            label: '下图标',
                            children:
                              <StyleWrapper form={form} preKeys={['addform', 'addform_number_icon_down', 'default']} onStyleChange={() => upateStyleEvent(['addform', 'addform_number_icon_down'])} />,
                          },
                        ]} />
                    },
                    {
                      key: 'select',
                      label: '单选',
                      children:
                        <Collapse accordion size='middle' items={[
                          {
                            key: 'input',
                            label: '单选',
                            children:
                              <StyleWrapper form={form} preKeys={['addform', 'addform_select', 'default']} onStyleChange={() => upateStyleEvent(['addform', 'addform_select'])} />,
                          },
                          {
                            key: 'focus',
                            label: '聚焦',
                            children:
                              <StyleWrapper form={form} preKeys={['addform', 'addform_select', 'focus']} onStyleChange={() => upateStyleEvent(['addform', 'addform_select'])} />,
                          },
                          {
                            key: 'icon',
                            label: '图标',
                            children:
                              <StyleWrapper form={form} preKeys={['addform', 'addform_select_icon', 'default']} onStyleChange={() => upateStyleEvent(['addform', 'addform_select_icon'])} />,
                          },
                        ]} />
                    },
                    {
                      key: 'date',
                      label: '日期',
                      children:
                        <Collapse accordion size='middle' items={[
                          {
                            key: 'input',
                            label: '日期',
                            children:
                              <StyleWrapper form={form} preKeys={['addform', 'addform_date', 'default']} onStyleChange={() => upateStyleEvent(['addform', 'addform_date'])} />,
                          },
                          {
                            key: 'focus',
                            label: '聚焦',
                            children:
                              <StyleWrapper form={form} preKeys={['addform', 'addform_date', 'focus']} onStyleChange={() => upateStyleEvent(['addform', 'addform_date'])} />,
                          },
                          {
                            key: 'icon',
                            label: '图标',
                            children:
                              <StyleWrapper form={form} preKeys={['addform', 'addform_date_icon', 'default']} onStyleChange={() => upateStyleEvent(['addform', 'addform_date_icon'])} />,
                          },
                        ]} />
                    },
                    {
                      key: 'face',
                      label: '人脸登录',
                      children:
                        <Collapse accordion size='middle' items={[
                          {
                            key: 'button',
                            label: '按钮',
                            children:
                              <StyleWrapper form={form} preKeys={['addform', 'addform_face', 'default']} onStyleChange={() => upateStyleEvent(['addform', 'addform_face'])} />,
                          },
                          {
                            key: 'hover',
                            label: 'hover',
                            children:
                              <StyleWrapper form={form} preKeys={['addform', 'addform_face', 'hover']} onStyleChange={() => upateStyleEvent(['addform', 'addform_face'])} />,
                          },
                        ]} />
                    },
                    {
                      key: 'fileupload',
                      label: '文件上传',
                      children:
                        <Collapse accordion size='middle' items={[
                          {
                            key: 'button',
                            label: '盒子',
                            children:
                              <StyleWrapper form={form} preKeys={['addform', 'addform_fileupload', 'default']} onStyleChange={() => upateStyleEvent(['addform', 'addform_fileupload'])} />,
                          },
                          {
                            key: 'hover',
                            label: 'hover',
                            children:
                              <StyleWrapper form={form} preKeys={['addform', 'addform_fileupload', 'hover']} onStyleChange={() => upateStyleEvent(['addform', 'addform_fileupload'])} />,
                          },
                        ]} />
                    },
                    {
                      key: 'textarea',
                      label: '多行文本',
                      children:
                        <Collapse accordion size='middle' items={[
                          {
                            key: 'input',
                            label: 'input',
                            children:
                              <StyleWrapper form={form} preKeys={['addform', 'addform_textarea', 'default']} onStyleChange={() => upateStyleEvent(['addform', 'addform_textarea'])} />,
                          },
                          {
                            key: 'icon',
                            label: 'focus',
                            children:
                              <StyleWrapper form={form} preKeys={['addform', 'addform_textarea', 'focus']} onStyleChange={() => upateStyleEvent(['addform', 'addform_textarea'])} />,
                          },
                        ]} />
                    },
                    {
                      key: 'ql',
                      label: '富文本',
                      children:
                        <Collapse accordion size='middle' items={[
                          {
                            key: 'box',
                            label: '盒子',
                            children:
                              <StyleWrapper form={form} preKeys={['addform', 'addform_ql', 'default']} onStyleChange={() => upateStyleEvent(['addform', 'addform_ql'])} />
                          },
                          {
                            key: 'header',
                            label: '工具栏',
                            children:
                              <StyleWrapper form={form} preKeys={['addform', 'addform_ql_header', 'default']} onStyleChange={() => upateStyleEvent(['addform', 'addform_ql_header'])} />
                          },
                          {
                            key: 'content',
                            label: '内容',
                            children:
                              <StyleWrapper form={form} preKeys={['addform', 'addform_ql_container', 'default']} onStyleChange={() => upateStyleEvent(['addform', 'addform_ql_container'])} />
                          },
                        ]} />
                    }
                  ]} />
              }
            ]} />
          </>

      },
      {
        key: 'footer',
        label: '底部按钮',
        children:
          <Collapse accordion size='middle' items={[
            {
              key: 'box',
              label: '盒子',
              children: <StyleWrapper form={form} preKeys={['footer', 'default']} onStyleChange={() => upateStyleEvent(['footer'])} />
            },
            {
              key: 'cancel',
              label: '返回按钮',
              children:
                <>
                  <Form.Item
                    label="文本"
                    name={['base', 'cancelText']}
                  >
                    <Input
                      onChange={(e) => onConfigEvent(['base', 'cancelText'], e.target.value)}
                    />

                  </Form.Item>

                  <StyleWrapper form={form} preKeys={['cancel_btn', 'default']} onStyleChange={() => upateStyleEvent(['cancel_btn'])} />

                </>
            },
            {
              key: 'cancel-hover',
              label: '返回按钮-悬浮',
              children:
                <StyleWrapper form={form} preKeys={['cancel_btn', 'hover']} onStyleChange={() => upateStyleEvent(['cancel_btn'])} />

            },
            {
              key: 'ok',
              label: '提交按钮',
              children:
                <>
                  <Form.Item
                    label="文本"
                    name={['base', 'okText']}
                  >
                    <Input
                      onChange={(e) => onConfigEvent(['base', 'okText'], e.target.value)}
                    />

                  </Form.Item>
                  <StyleWrapper form={form} preKeys={['ok_btn', 'default']} onStyleChange={() => upateStyleEvent(['ok_btn'])} />

                </>
            },
            {
              key: 'ok-hover',
              label: '提交按钮-悬浮',
              children:
                <StyleWrapper form={form} preKeys={['ok_btn', 'hover']} onStyleChange={() => upateStyleEvent(['ok_btn'])} />

            }
          ]} />
      },
    ]

    return items

  }, [updateCtx.updateFlag])

  useEffect(() => {
    setAttrForm(form)
  }, [form])

  return (
    <>

      <Form
        name='add'
        form={form}
        initialValues={template2}
        labelCol={{
          span: 6,
        }
        }
        autoComplete="off"
        spellCheck="false"
      >

        {/* 按钮区域 */}
        <AttrHeaderBtn onClickEvent={handleHeaderEvent} />

        {/* 对象形式的样式编辑 */}
        <Collapse accordion destroyInactivePanel size='middle' items={items} />


      </Form>

      {/* 字符串形式的样式编辑 */}
      <CssEdit value={template2.css} open={open} onValueChange={cssChange} setOpen={setOpen} ref={cssEditRef} />

    </>

  )
}
export default React.memo(AddAttr) 